import java.util.*;

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Test3 {


    /**
     * 去除数组中重复元素
     * @param array
     */
    public static void duplicateRemoval(int[] array) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        System.out.println(set);
    }

    /**
     * 字符串中第一个只出现一次的字符
     * @param array
     */
    public static void firstRepeat(int[] array) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if (!set.contains(array[i])) {
                set.add(array[i]);
            } else {
                System.out.println(array[i]);
                return;
            }
            System.out.println(set);
        }
    }

    /**
     * 统计数组中次数超过一次的 数字
     * @param array
     */
    public static void countReapt(int[] array) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int x : array) {
            if (map.get(x) == null) {
                map.put(x, 1);
            } else {
                int val = map.get(x);
                map.put(x, val + 1);
            }
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println(entry.getKey() + " " + entry.getValue());
            }
        }

    }

    public static void main(String[] args) {
        int[] array = {11, 22, 11,};
        System.out.println(singleNumber(array));
    }

    /**
     * 只出现一次的数字
     * @param nums
     * @return
     */
    public static int singleNumber(int[] nums) {
        Set<Integer> set = new TreeSet<>();
        for (int x : nums) {
            if (set.contains(x)) {
                set.remove(x);
            } else {
                set.add(x);
            }
        }
        for (int x : nums) {
            if (set.contains(x)) {
                return x;
            }
        }
        return -1;
    }

    /**
     * 复制带随机指针的链表
     * @param head
     * @return
     */
    public static Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            Node node = new Node(cur.val);
            map.put(cur, node);
            cur = cur.next;
        }
        cur = head;
        while (cur!=null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);

    }






}
